<?php



namespace app\mini\model;



use think\facade\Db;

use app\common\traits\ActionRecordTrait;

use think\Model;



class CustomerService extends Model

{

	use ActionRecordTrait;



    /**

     * 表名

     * @var string

     */

    protected $name = 'service';

    protected $deleteTime = 'deleted_at';

    protected $defaultSoftDelete = 0;



    /**

     * 获取服务列表

     * @param  array $request 请求参数

     * @return array

     * @author hz

     * @create 2021-10-19

     */

    public function getServiceList($request)

    {

        try {

            $customer_id = $request['customer_id'] ?? 0;

            // $customer_id = 1;

            $service_id = $request['service_id'] ?? 0;

            if(!$customer_id)

            {

            	return ['error' => '您还未认证,请先认证'];

            }

            $where   = [];

            $where = [

                ['customer_id', '=', $customer_id]

            ];

            // 从服务记录进入某一个服务，需要服务id

            if($service_id)

            {

                array_push($where, ['id', '=', $service_id]);

            }



            // $field = ['*'];

            $list  = Db::name('service')->where($where)->limit(($request['page'] - 1) * $request['limit'], $request['limit'])->order('id DESC')->select()->toArray();

            $dataCount = Db::name('service')->where($where)->count();



            if ($list) 

            {

                // 计算已服务次数和未服务次数

                $service_ids = array_column($list, 'id');

                $swhere = [];

                $swhere = [

                    ['service_id', 'in', $service_ids]

                ];

                $service_log_list = Db::name('service_log')

                    ->field('count(service_id) as service_num')

                    ->where($swhere)

                    ->group('service_id')

                    ->select()->toArray();



                foreach($list as &$v){

                    if($v['service_type'] == 2)

                    {

                        $v['service_num'] = $service_log_list[$v['id']] ?? 0; //已服务次数

                        $v['un_service_num'] = $v['num'] - $v['service_num']; //未服务次数

                        $v['service_type_name'] = '按次服务';

                        // 服务状态

                        if($v['service_num'] == 0)

                        {

                            $v['service_status_name'] = '未开始';

                        }

                        if($v['service_num'] >0 &&  $v['service_num'] < $v['num'])

                        {

                            $v['service_status_name'] = '进行中';

                        }

                        if($v['service_num'] == $v['num'])

                        {

                            $v['service_status_name'] = '已完成';

                        }

                    }

                    else{

                        $v['service_num'] = $service_log_list[$v['id']] ?? 0; //已服务次数

                        $v['un_service_num'] = ''; //未服务次数

                        $v['service_type_name'] = '按期服务';

                        // 服务状态

                        if( $v['start_time'] > time() )

                        {

                            $v['service_status_name'] = '未开始';

                        }

                        if( $v['start_time'] < time()  && $v['end_time'] > time() )

                        {

                            $v['service_status_name'] = '进行中';

                        }

                        if($v['end_time'] < time())

                        {

                            $v['service_status_name'] = '已完成';

                        }

                    }

                    $v['start_time'] = $v['start_time'] ? date('Y-m-d',$v['start_time']) : '';

                    $v['end_time'] = $v['end_time'] ? date('Y-m-d',$v['end_time']) : '';

                    

                }

            }



            $data              = [];

            $data['list']      = $list ??［］;

            $data['dataCount'] = $dataCount ?? 0;

            return ['data' => $data];

        } catch (\Exception $e) {

            return ['error' => $e->getMessage()];

        }

    }



    /**

     * 获取服务记录列表

     * @param  array $request 请求参数

     * @return array

     * @author hz

     * @create 2021-10-19

     */

    public function getServiceLogList($request)

    {

        try {

            $customer_id = $request['customer_id'] ?? 0;

            // $customer_id = 1;

            $service_id = $request['service_id'] ?? 0;

            if(!$customer_id)

            {

                return ['error' => '您还未认证,请先认证'];

            }

            $where   = [];

            $where = [

                ['s.customer_id', '=', $customer_id]

            ];

            // 从服务进入某一个服务的服务记录，需要服务id

            if($service_id)

            {

                array_push($where, ['sl.service_id', '=', $service_id]);

            }



            $field = ['sl.create_time,sl.nurse_id_one,sl.nurse_id_two,sl.id'];

            $sqlQuery = Db::name('service_log')->alias('sl')

                    ->join('service s', 's.id = sl.service_id', 'LEFT');

            $list = $sqlQuery->field($field)->where($where)->limit(($request['page'] - 1) * $request['limit'], $request['limit'])->order('sl.id DESC')->select()->toArray();



            $dataCount = $sqlQuery->where($where)->count();



            if ($list) 

            {

                // 护理师

                $nurse_id_one = array_column($list, 'nurse_id_one');

                $nurse_id_two = array_column($list, 'nurse_id_two');

                $nurse_id_one = is_array($nurse_id_one) ? $nurse_id_one : [];

                $nurse_id_two = is_array($nurse_id_two) ? $nurse_id_two : [];

                $nurse_ids = array_unique(array_merge($nurse_id_one,$nurse_id_two));

                $pwhere = [];

                $pwhere = [

                    ['id', 'in', $nurse_ids]

                ];

                $nurse_name = Db::name('shop_person')->where($pwhere)->column('name','id');



                foreach($list as &$v){

                    // $v['star_time'] = $v['star_time'] ? date('Y-m-d',$v['star_time']) : '';

                    // $v['end_time'] = $v['end_time'] ? date('Y-m-d',$v['end_time']) : '';

                    $v['create_time'] = $v['create_time'] ? date('Y-m-d H:i:s',$v['create_time']) : '';

                    $v['nurse_name_one'] = $nurse_name[$v['nurse_id_one']] ?? '';

                    $v['nurse_name_two'] = $nurse_name[$v['nurse_id_two']] ?? '';

                    

                }

            }



            $data              = [];

            $data['list']      = $list ?? [];

            $data['dataCount'] = $dataCount ?? 0;

            return ['data' => $data];

        } catch (\Exception $e) {

            return ['error' => $e->getMessage()];

        }

    }

}